package com.qin.dao;

import java.sql.SQLException;
import java.util.List;

import com.qin.entity.BookCategory;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanHandler;

/**
 * 操作BookCategory实体类
 * 
 * @author BestRiven
 *
 */
public class BookCategoryDao {

	/**
	 * 插入图书分类
	 * @param bookCategory 要插入的图书分类
	 * @return	返回自增id
	 */
	public Long insertBookCategory(BookCategory bookCategory) {
		Entity record = Entity.parseWithUnderlineCase(bookCategory);
		
		Long insertForGeneratedKey = 0L;
		try {
			insertForGeneratedKey = Db.use().insertForGeneratedKey(record);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return insertForGeneratedKey;
	}
	
	/**
	 * 逻辑删除指定id的图书分类（仅改变图书分类的状态）
	 * @param id 要逻辑删除的图书分类的id
	 * @return 影响行数（0代表失败，1代表影响一行）
	 */
	public int logicDeleteBookCategory(Long id) {
		String sql = "UPDATE `book_category`  SET `status` = 0 WHERE `id` = ?";
		
		int execute = 0;
		try {
			execute = Db.use().execute(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return execute;
	}
	
	/**
	 * 拿到要修改的图书分类的id（并且满足status=1），根据传过来的图书分类动态sql拼串，修改图书分类
	 * @param id 要修改的图书分类id
	 * @param bookCategory 封装图书分类要修改的属性
	 * @return 影响行数（0代表失败，1代表影响一行）
	 */
	public int updateBookCategory(Long id,BookCategory bookCategory) {
		Entity record = Entity.parse(bookCategory, true, true);
		Entity where = Entity.create("book_category").set("id", id).set("status", 1);
		
		int update = 0;
		try {
			update = Db.use().update(record, where);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return update;
	}
	
	/**
	 * 根据图书分类的id（并且满足status=1）查询对应的图书分类
	 * @param id 图书分类的id
	 * @return 查询得到的图书分类（未查到返回null）
	 */
	public BookCategory queryBookCategoryById(Long id) {
		Entity entity = Entity.create("book_category").set("id", id).set("status", 1);
		BookCategory bookCategory = null;
		try {
			bookCategory = Db.use().find(entity,new BeanHandler<BookCategory>(BookCategory.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return bookCategory;
	}
	
	/**
	 * 通过分类等级来查询图书分类
	 * @param level 图书分类的等级
	 * @return 该等级下的所有图书分类
	 */
	public List<BookCategory> queryBookCategoryByCLevel(Integer level){
		List<BookCategory> bookCategories = null;
		Entity where = Entity.create("book_category").set("cat_level", level).set("status", 1);
		try {
			bookCategories = Db.use().find(where,BookCategory.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return bookCategories;
	}
	/**
	 * 通过父id来查询图书分类
	 * @param id 图书分类的父id
	 * @return 父id下的所有图书分类
	 */
	public List<BookCategory> queryBookCategoryByPId(Long id){
		List<BookCategory> bookCategories = null;
		Entity where = Entity.create("book_category").set("parent_id", id).set("status", 1);
		try {
			bookCategories = Db.use().find(where,BookCategory.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return bookCategories;
	}
	
	/**
	 * 查询所有的图书分类
	 * @return 所有的图书分类，用List封装
	 */
	public List<BookCategory> queryAllBookCategory(){
		List<BookCategory> bookCategories = null;
		try {
			bookCategories = Db.use().find(Entity.create("book_category").set("status", 1),BookCategory.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return bookCategories;
	}
}
